<?php

namespace App\Containers\AppSection\LegalForm\Tasks;

use App\Containers\AppSection\LegalForm\Data\Repositories\LegalFormRepository;
use App\Containers\AppSection\LegalForm\Models\LegalForm;
use App\Ship\Exceptions\CreateResourceFailedException;
use App\Ship\Parents\Tasks\Task;

class CreateDefaultLegalFormTask extends Task
{
    protected LegalFormRepository $repository;

    public function __construct(LegalFormRepository $repository)
    {
        $this->repository = $repository;
    }

    public function run(int $userId, $passportId, $personalId, $companyId = null): LegalForm
    {
        try {
            return $this->repository->create([
                'user_id' => $userId,
                'passport_id' => $passportId,
                'personal_id' => $personalId,
                'company_id' => $companyId,
            ]);
        } catch (Exception $exception) {
            throw new CreateResourceFailedException('Failed to create LegalForm');
        }
    }
}
